<template>
  <view class="container">
    <uni-nav-bar
      :border="false"
      statusBar
      title="详情"
      color="#000"
      left-icon="left"
      backgroundColor="transparent"
      @clickLeft="onBack"
    >
    </uni-nav-bar>

    <view class="box">
      <view class="mainBox">
        <view class="id" v-if="data?.patrolItems">
          <view
            class="tag"
            v-for="(item, i) in data?.patrolItems.split('|')"
            :key="i"
          >
            {{ item }}
          </view>
        </view>
        <!-- <view class="val">{{ data?.patrolItems }}</view> -->
        <view class="status" v-if="data.isFinish == 0"
          >巡检{{ data.isFinish == 0 ? '未结束' : '已结束' }}</view
        >
        <view class="status" v-else>巡检{{ data?.finishType }}结束</view>
      </view>
      <view class="infoBox">
        <view class="item">
          <text class="name">开始时间</text>
          <text class="val">{{ data?.startTime }}</text>
        </view>
        <view class="item">
          <text class="name">结束时间</text>
          <text class="val">{{ data?.endTime }}</text>
        </view>
        <view class="item">
          <text class="name">应巡检(个)</text>
          <text class="val">{{ data?.shouldPatrolNum }}</text>
        </view>
        <view class="item">
          <text class="name">已巡检(个)</text>
          <text class="val">{{ data?.alreadyPatrolNum }}</text>
        </view>
        <view class="item">
          <text class="name">未巡检(个)</text>
          <text class="val">{{ data?.notPatrolNum }}</text>
        </view>

        <view class="item">
          <text class="name">预定巡检人</text>
          <view class="val">
            <text v-for="(n, i) in data.patrolUserIds" :key="i"
              >{{ $util.selectDictLabel(personData, n)
              }}{{ data.patrolUserIds.length === i + 1 ? '' : ',' }}</text
            >
          </view>
        </view>
      </view>
    </view>
    <view class="footer">
      <button class="btn" type="primary" @click="onDetail">明细</button>
      <!-- <button
        class="btn"
        type="warn"
        v-if="data.isFinish == 0"
        @click="onFinish"
      >
        结束巡检
      </button> -->
    </view>

    <uni-popup ref="popup">
      <FormView :data="data" @callback="onCallback" />
    </uni-popup>
  </view>
</template>

<script>
import { getUserAllList } from '@/api/index';
import {
  getPatrolRecord,
  editPatrolRecord,
  getPatrolRecordDetailList,
} from '@/api/patrol';
import FormView from './formFinish.vue';
import useAppStore from '@/store/modules/app';
export default {
  components: {
    FormView,
  },
  data() {
    return {
      personData: [],
      data: {},
      query: {},
      // store
      appStore: useAppStore(),
    };
  },
  onLoad(query) {
    this.query = query;
    this.init();
    this.getData();
  },
  methods: {
    init() {
      getUserAllList().then((res) => {
        this.personData = res.data.map((item) => {
          return {
            ...item,
            text: item.nickName,
            label: item.nickName,
            value: item.userId,
          };
        });
      });
    },
    getData() {
      getPatrolRecord(this.query.patrolRecordId).then((res) => {
        this.data = res.data;
      });
    },
    // 结束
    onFinish() {
      let content = '是否确认结束？';
      if (this.data.notPatrolNum && this.data.notPatrolNum > 0) {
        content = '还有未巡检项目，是否结束?';
      }
      uni.showModal({
        title: '提示',
        content: content,
        success: (res) => {
          if (res.confirm) {
            this.$refs.popup.open('bottom');
          } else if (res.cancel) {
            console.log('用户点击取消');
          }
        },
      });
    },
    onCallback(r) {
      // loading提示
      uni.showLoading({ title: '提交中' });
      let params = {
        ...this.data,
        ...r,
      };
      editPatrolRecord(params).then((res) => {
        uni.hideLoading();
        uni.showToast({
          title: `操作成功`,
        });
        // 刷新详情
        this.getData();
        this.$refs.popup.close();
      });
    },
    // 明细
    onDetail() {
      // url参数
      let obj = {
        ...this.query,
      };
      let str = this.$util.jsonToUrlParam(obj);
      this.onLocation(`/pages/patrol/detailList?${str}`);
    },
    // 跳转页面
    onLocation(url) {
      uni.navigateTo({
        url,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  min-height: calc(100vh - 160rpx);
  padding-bottom: calc(
    var(--window-bottom) + constant(safe-area-inset-bottom) + 160rpx
  );
  padding-bottom: calc(
    var(--window-bottom) + env(safe-area-inset-bottom) + 160rpx
  );
  background: linear-gradient(180deg, #d7e4fd 0%, #fff 50%);
  background-repeat: no-repeat;
  background-size: 100% auto;
  position: relative;
  .box {
    margin: 50rpx 36rpx 0;
    padding: 24rpx;
    background: #ffffff;
    border-radius: 12rpx;
    box-shadow: 0 0 30rpx 0 #ccc;
    .mainBox {
      text-align: center;
      padding: 40rpx 0;
      line-height: 1;

      .id {
        font-size: 38rpx;
        font-weight: bold;
        color: #4288ff;
        padding-bottom: 40rpx;
        .tag {
          border: 1rpx solid;
          border-radius: 4rpx;
          padding: 10rpx 0;
          margin-bottom: 6rpx;
          &:last-child {
            margin-bottom: 0;
          }
        }
      }

      .val {
        font-size: 40rpx;
        font-weight: 800;
        color: #ff884d;
        padding-bottom: 27rpx;

        text {
          font-size: 30rpx;
          margin-left: 10rpx;
        }
      }

      .status {
        font-size: 26rpx;
        font-weight: 500;
        color: #555555;
      }
    }
    .infoBox {
      .item {
        font-size: 30rpx;
        font-weight: 500;
        padding: 22rpx 12rpx;
        display: flex;
        justify-content: space-between;
        border-bottom: 2rpx solid #eaeaea;
        &:last-child {
          border-bottom: 0;
        }
        .name {
          color: #989898;
        }

        .val {
          width: 60%;
          color: #333;
          text-align: right;
          word-break: break-all;
        }
      }
    }
  }
  .myLabel {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .star {
      color: #dd524d;
    }
  }
  .footer {
    display: flex;
    justify-content: space-evenly;
    position: fixed;
    left: 0;
    right: 0;
    bottom: calc(
      var(--window-bottom) + constant(safe-area-inset-bottom) + 60rpx
    );
    bottom: calc(var(--window-bottom) + env(safe-area-inset-bottom) + 60rpx);
    .btn {
      width: 40%;
      margin: 0;
    }
  }
}
</style>
